package hutoolandguava;

import com.google.common.collect.*;
import org.apache.commons.collections4.multiset.HashMultiSet;

import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ZH
 */
public class GuavaMultiTest {
    public static void main(String[] args) {
        //hashMultimap();
        //hashMultiset();
        //biMap();
        table();
    }
    public static void hashMultimap(){
        //value是一个集合  HashMultimap<Integer, Integer> 可以代替 HashMap<Integer, Set<Integer>>
        HashMultimap<Integer, Integer> map = HashMultimap.create();
        map.put(1,10086);
        map.put(2,2);
        map.put(1,10087);//第二次插入
        map.put(1,10088);//第三次插入
        Set<Integer> keys = map.get(1);
        System.out.println(keys);//[10088, 10086, 10087]
        System.out.println(map); //{1=[10088, 10086, 10087], 2=[2]}
    }
    public static void hashMultiset(){
        //可重复的set
        HashMultiset<Integer> multiset = HashMultiset.create();
        //ConcurrentHashMultiset.create() 线程安全的set
        multiset.add(1);
        multiset.add(1);
        multiset.add(2);
        multiset.add(3);
        multiset.add(1);
        System.out.println(multiset.count(1));// 3
        System.out.println(multiset.count(4));// 0
        System.out.println(multiset);//[1 x 3, 2, 3]
    }
    public static void biMap(){
        //双向查找map
        //key 和 value 都不可以重复
        HashBiMap<Integer, Long> biMap = HashBiMap.create();
        biMap.put(1,100L);
        biMap.put(2,200L);
        biMap.put(3,300L);
        biMap.put(4,400L);
        BiMap<Long, Integer> inverse = biMap.inverse();//反转key 和 value
        System.out.println(biMap.get(1));
        System.out.println(inverse.get(100L));
    }
    public static void table(){
        Table<String,String,Integer> tables=HashBasedTable.create();
        tables.put("a", "javase", 80);
        tables.put("b", "javaee", 90);
        tables.put("c", "javaee", 100);
        tables.put("d", "guava", 70);
        tables.put("a", "guava1", 70);
        Set<Table.Cell<String, String, Integer>> cells = tables.cellSet();
        System.out.println(cells);
        System.out.println(tables.get("a", "guava1"));
    }
}
